﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BaseUi.Device
{
    public class VisionBan
    {
        public Action<string> recact;
        public string ReceiveMes;
        public bool connect = false;
        bool stop = false;
        Socket robotclient;
        string robotip;
        int robotport;
        public Action<string> Status;
        public VisionBan(string IP, int Port)
        {
            try
            {

                robotip = IP;
                robotport = Port;
                robotclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Task.Factory.StartNew(() =>
                {
                    while (!stop)
                    {
                        if (!connect)
                        {
                            try
                            {
                                //DisConnect();
                                robotclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                                IPAddress pAddress = IPAddress.Parse(IP);
                                robotclient.Connect(pAddress, Port);
                                connect = true;

                            }
                            catch (Exception ex)
                            {
                                connect = false;
                            }
                        }
                        System.Threading.Thread.Sleep(200);
                    }
                });
            }
            catch (Exception)
            {
                connect = false;
            }
        }
        object locksend = new object();
        public string SetVariable(string mes, string variable)
        {
            lock (locksend)
            {
                try
                {
                    string middle = "\"" + variable + "\"";
                    string sendmes = mes + middle;
                    //"$SetVariable,AllWkConfig1$,\"O#0;O#1;O#1;O#1;O#1;O#1;O#1;O#1\""
                    //"$SetVariable,AllWkConfig1$,\"stationlist1\""
                    // $SetVariable,AllWkConfig1$,\"3#1#1#1#1#1#1#1#0#\"
                    if (robotclient.Available > 0)
                    {
                        int reclength1 = robotclient.Available;
                        byte[] recbyt1 = new byte[reclength1];
                        robotclient.Receive(recbyt1);
                    }
                    byte[] sendb = Encoding.Default.GetBytes(sendmes + "\r\n");
                    robotclient.Send(sendb);
                    while (robotclient.Available == 0)
                    {
                        Thread.Sleep(20);
                    }
                    int reclength = robotclient.Available;
                    byte[] recbyt = new byte[reclength];
                    robotclient.Receive(recbyt);
                    ReceiveMes = Encoding.Default.GetString(recbyt);
                    return ReceiveMes;
                }
                catch (Exception)
                {
                    connect = false;
                    return "";
                }
            }
        }
        public string SetV2(string mes)
        {
            lock (locksend)
            {
                try
                {
                    byte[] sendb = Encoding.Default.GetBytes(mes + "\r\n");
                    robotclient.Send(sendb);
                    while (robotclient.Available == 0)
                    {
                        Thread.Sleep(20);
                    }
                    int reclength = robotclient.Available;
                    byte[] recbyt = new byte[reclength];
                    robotclient.Receive(recbyt);
                    ReceiveMes = Encoding.Default.GetString(recbyt);
                    return ReceiveMes;
                }
                catch (Exception)
                {
                    connect = false;
                    return "";
                }
            }
        }
        public string SendMes(string mes)
        {
            lock (locksend)
            {
                try
                {
                    DateTime dt1 = DateTime.Now;
                    string sendmes = mes;
                    byte[] sendb = Encoding.Default.GetBytes(mes);

                    if (robotclient.Available > 0)
                    {
                        int reclength1 = robotclient.Available;
                        byte[] recbyt1 = new byte[reclength1];
                        robotclient.Receive(recbyt1);
                    }

                    robotclient.Send(sendb);
                    while (robotclient.Available == 0)
                    {
                        DateTime dt2 = DateTime.Now;
                        if ((dt2 - dt1).Seconds > 10)
                        {
                            return "ng";
                        }
                        Thread.Sleep(20);
                    }
                    int reclength = robotclient.Available;
                    byte[] recbyt = new byte[reclength];
                    robotclient.Receive(recbyt);
                    ReceiveMes = Encoding.Default.GetString(recbyt);
                    Logs.Write("视觉通信地址" + robotip + ":" + robotport + "\r\n发" + mes + "\r\n接收" + ReceiveMes, LogsType.vision);
                    return ReceiveMes;
                }
                catch (Exception ex)
                {
                    Logs.Write("视觉通信地址" + robotip + ":" + robotport + "\r\n发" + mes + "\r\n接收故障" + ex.ToString(), LogsType.vision);
                    connect = false;
                    return ex.ToString();
                }
            }
        }
        ~VisionBan()
        {
            robotclient?.Close();
        }
        public bool DisConnect()
        {
            try
            {
                stop = true;
                robotclient?.Close();

                return true;
            }
            catch (Exception)
            {
                stop = true;
                return false;
            }
        }
    }
}
